﻿#include "collectionlist.h"
#include "ui_collectionlist.h"
#include <QDebug>

CollectionList::CollectionList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CollectionList)
{
    ui->setupUi(this);

    m_contentCount = 0;
}

CollectionList::~CollectionList()
{
    delete ui;
}

void CollectionList::slotSetSaveList(bool result,const QString& msg)
{
    if (result)
    {
        qDebug() << "slotSetSaveList " << "m_contentCount = " << m_contentCount;
        CollectionContent *collectionContent = new CollectionContent();
        m_collectionContentVector.append(collectionContent);
        ui->lwg_content->addItem("");
        ui->lwg_content->setItemWidget(ui->lwg_content->item(m_contentCount),collectionContent);
        ui->lwg_content->item(m_contentCount)->setSizeHint(QSize(0, 120));  //每次改变Item的高度
        m_contentCount++;

        connect(collectionContent,SIGNAL(signalGainHotelDetails(const QString&)),
                this, SIGNAL(signalGainHotelDetailsCollection(const QString&)));
        connect(collectionContent,SIGNAL(signalDeleteSaveHotel(const QString&)),
                this, SIGNAL(signalDeleteSaveHotelCollection(const QString&)));
        connect(collectionContent,SIGNAL(signalDeleteSaveHotel(const QString&)),
                this, SLOT(slotSaveListClean()));

        //    connect(collectionContent,SIGNAL(signalReserveHotel(QString)),
        //            this, SLOT(slotReserveHotel(QString)));

    }
}

void CollectionList::slotSetSaveHotelImage(bool result,const QString& saveID,const QImage& image)
{
    if (result)
    {
        for (int i = 0; i < m_contentCount; i++)
        {
//            qDebug() << image.size() << i  << m_collectionContentVector.size();
            if (saveID == m_collectionContentVector[i]->getSaveId())
            {
                m_collectionContentVector[i]->setImage(image);
            }
        }
    }
}

void CollectionList::slotSaveListClean()
{
    if (m_contentCount == 0) /// 释放 delete
    {
        return;
    }

    while (ui->lwg_content->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_content->takeItem(0);
        delete item;
    }
    m_contentCount = 0;
    m_collectionContentVector.clear();
}
